package com.yn.iotxiao.Utils;


import cn.hutool.core.util.TypeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JsonHelper {

    public static Object getWithKey(JSONObject jsonObject, String key) {
        if (!jsonObject.containsKey(key)) {
            return  "";
        }
        return  jsonObject.get(key);
    }

    public static String getValue(JSONObject json, String key) {
        if (json.containsKey(key)) {
            return  json.get(key).toString();
        }
        return  "";
    }


    /**
     * @param datas  key,value,key,value 成对出现
     * @return
     */
    public static JSONObject create(Object... datas) {

        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < datas.length; i+=2) {
            String key = datas[i].toString();
            Object value = datas[i+1];
            jsonObject.put(key,value);
        }
        return jsonObject;
    }

//    public static String hikFormat(JSONObject jsonObject) {
////        "{\n" +
////                "    \"UserInfoDelCond\": {\n" +
////                "        \"EmployeeNoList\": [\n" +
////                "            {\n" +
////                "                \"employeeNo\": \"111\"\n" +
////                "            }\n" +
////                "        ]\n" +
////                "    }\n" +
////                "}"
//
//        String data = "{\n";
//
//        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
//            System.out.println(entry.getKey() + entry.getValue());
//
//            String start = "\"";
//            String end = "\"";
//            if (entry.getValue() instanceof JSONArray) {
//                start = "[\n";
//                end = "]\n";
//            }else if (entry.getValue() instanceof JSON) {
//                start = "{\n";
//                end = "}\n";
//            }
//            data += "\"" + entry.getKey() + "\n:"
//                    + start
//        }
//    }

    public static String obj2json(Object obj) {
        return obj2json(obj, DateUtil.DATE_TIME_PATTERN);
    }

    /**
     *
     * @Description:
     * @param obj
     * @param beIgnoreNull 是否忽略null值
     * @param beFormat  是否格式化
     * @param dataFormat  指定日期格式化
     * @return
     */
    public static String obj2json2(Object obj, boolean beIgnoreNull, boolean beFormat, String dataFormat) {
        List<SerializerFeature> features = new ArrayList<>();
        if (!beIgnoreNull) {
            features.add(SerializerFeature.WriteMapNullValue);
            features.add(SerializerFeature.DisableCircularReferenceDetect);
            features.add(SerializerFeature.WriteNullStringAsEmpty);
            features.add(SerializerFeature.WriteNullListAsEmpty);
            features.add(SerializerFeature.WriteDateUseDateFormat);
        }
        if (beFormat) {
            features.add(SerializerFeature.PrettyFormat);
        }
        features.add(SerializerFeature.WriteDateUseDateFormat);
        return obj2json2(obj, dataFormat, features.toArray(new SerializerFeature[] {}));
    }

    public static String obj2json2(Object obj, boolean beIgnoreNull, boolean beFormat) {
        return obj2json2(obj, beIgnoreNull, beFormat, DateUtil.DATE_TIME_PATTERN);
    }

    public static String obj2json2(Object obj, boolean beIgnoreNull) {
        return obj2json2(obj, beIgnoreNull, false);
    }

    public static String obj2json2(Object obj) {
        return obj2json2(obj, false);
    }

    /**
     *
     * @Description: 对象转json
     * @param obj
     *            对象
     * @param beFormat
     *            是否格式化
     * @return json字符串
     */
    public static String obj2json(Object obj, boolean beFormat) {
        return obj2json2(obj, false, beFormat);
    }

    /**
     *
     * @Description: 对象转json
     * @param obj
     * @param dataFormat 日期格式
     * @return
     *
     * 如果java bean的Date类型属性需要特殊处理，使用注解
     * @com.alibaba.fastjson.annotation.JSONField
     */
    public static String obj2json(Object obj, String dataFormat) {
        return obj2json2(obj, false, false, dataFormat);
    }

    private static String obj2json2(Object obj, String dataFormat, SerializerFeature... feature) {
        String defaultFormat = JSONObject.DEFFAULT_DATE_FORMAT;
        JSONObject.DEFFAULT_DATE_FORMAT = dataFormat;
        String json = JSON.toJSONString(obj, feature);
        JSONObject.DEFFAULT_DATE_FORMAT = defaultFormat;
        return json;
    }

    /**
     *
     * @Description: xml转json
     * @param xml
     * @return json字符串
     */
    public static String xml2json(String xml) {
        return cn.hutool.json.JSONUtil.xmlToJson(xml).toJSONString(0);
    }

    /**
     *
     * @Description: json转对象
     * @param jsonString
     * @param beanClass
     * @return 实体类对象
     */
    public static <T> T json2Obj(String jsonString, Class<T> beanClass) {
        return JSON.parseObject(jsonString, beanClass);
    }

    /**
     *
     * @Description JSON字符串转为实体类对象，转换异常将被抛出
     *
     * @param <T>
     *            Bean类型
     * @param jsonString
     *            JSON字符串
     * @param typeReference
     *            {@link TypeReference}类型参考子类，可以获取其泛型参数中的Type类型
     * @param ignoreError
     *            是否忽略错误
     * @return 实体类对象
     */
    public static <T> T json2Obj(String jsonString, TypeReference<T> typeReference,
                                 boolean ignoreError) {
        /* return cn.hutool.json.JSONUtil.toBean(jsonString, TypeUtil.getTypeArgument(typeReference.getClass()),
            ignoreError);*/
        return json2Obj(jsonString, typeReference);
    }

    /**
     *
     * @Description:
     * @param <T>
     * @param jsonString
     * @param typeReference
     * @return
     *
     * eg: JsonUtil.json2Obj(jsonString, new com.sf.nwms.core.util.lang.TypeReference<List<JavaBean>())
     */
    public static <T> T json2Obj(String jsonString, TypeReference<T> typeReference) {
        return JSON.parseObject(jsonString, TypeUtil.getTypeArgument(typeReference.getClass()));
    }

    /**
     *
     * @Description: json转对象
     * @param jsonString
     *            json字符串
     * @param beanType
     *            实体类对象类型
     * @param ignoreError
     *            是否忽略错误
     * @return 实体类对象
     */
    public static <T> T json2Obj(String jsonString, Type beanType, boolean ignoreError) {
        return JSON.parseObject(jsonString, beanType);
    }
}
